package com.cardone.platform.common.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dto.EntityLogDto;
import com.cardone.platform.common.service.EntityLogService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 实体日志
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/common/entityLog/")
@Controller
public class EntityLogController {

	/**
	 * 实体日志：删除
	 * 
	 * @param deleteEntityLog
	 *            实体日志对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
  public Callable<ReturnDataDto<String>> deleteJson(final EntityLogDto deleteEntityLog) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityLogService.class).deleteByIds(deleteEntityLog);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 实体日志：编辑
	 * 
	 * @param findEntityLog
	 *            实体日志对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<EntityLogDto>> findByIdJson(final EntityLogDto findEntityLog, final Model model) {
		return new Callable<ReturnDataDto<EntityLogDto>>() {
			@Override
			public ReturnDataDto<EntityLogDto> call() throws Exception {
				try {
					final EntityLogDto entityLog = ContextHolder.getBean(EntityLogService.class).findById(EntityLogDto.class, findEntityLog);

					return ReturnDataUtils.newReturnData(entityLog);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityLogController.log);
				}
			}
		};
	}

	/**
	 * 实体日志：分页查询
	 * 
	 * @param paginationEntityLog
	 *            实体日志对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
  public Callable<ReturnDataDto<PaginationDto<EntityLogDto>>> paginationByLikeCodeJson(final EntityLogDto paginationEntityLog) {
		return new Callable<ReturnDataDto<PaginationDto<EntityLogDto>>>() {
			public ReturnDataDto<PaginationDto<EntityLogDto>> call() throws Exception {
				try {
					final PaginationDto<EntityLogDto> entityLogPagination = ContextHolder.getBean(EntityLogService.class).paginationByLikeCode(EntityLogDto.class, paginationEntityLog);
				
					return ReturnDataUtils.newReturnData(entityLogPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 实体日志：分页查询
	 * 
	 * @param paginationEntityLog
	 *            实体日志对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
  public void paginationHtml(final EntityLogDto paginationEntityLog, final Model model) {
		final PaginationDto<EntityLogDto> entityLogPagination = ContextHolder.getBean(EntityLogService.class).paginationByLikeCode(EntityLogDto.class, paginationEntityLog);
		
		model.addAttribute("entityLogPagination", entityLogPagination);
	}
	
	/**
	 * 实体日志：分页查询
	 * 
	 * @param findEntityLog
	 *            实体日志对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
  public void editHtml(final EntityLogDto findEntityLog, final Model model) {
		final EntityLogDto entityLog = ContextHolder.getBean(EntityLogService.class).findById(EntityLogDto.class, findEntityLog);
		
		model.addAttribute("entityLog", entityLog);
	}

	/**
	 * 实体日志：查询
	 * 
	 * @param findListEntityLog
	 *            实体日志对象
	 * @return 实体日志对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
  	public Callable<ReturnDataDto<List<EntityLogDto>>> findListByLikeCodeJson(final EntityLogDto findListEntityLog) {
		return new Callable<ReturnDataDto<List<EntityLogDto>>>() {
			public ReturnDataDto<List<EntityLogDto>> call() throws Exception {
				try {
					final List<EntityLogDto> entityLogList = ContextHolder.getBean(EntityLogService.class).findListByLikeCode(EntityLogDto.class, findListEntityLog);
				
					return ReturnDataUtils.newReturnData(entityLogList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 实体日志：保存
	 * 
	 * @param saveEntityLog
	 *            实体日志对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
  public Callable<ReturnDataDto<String>> saveJson(final EntityLogDto saveEntityLog) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityLogService.class).saveByIdOrCode(EntityLogDto.class, saveEntityLog);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}
}
